package cn.gmcx.lazy.izze.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtils {
    private final static String sign = "sbhzj";

    public static String ToJwt(Map<String, String> map) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR,24);
        JWTCreator.Builder builder = JWT.create();

        map.forEach((k, v)->{
            builder.withClaim(k,v);
        });

        String token = builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(sign));

        return token;
    }

    public static DecodedJWT parseJwt(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(sign)).build().verify(token);
        return verify;
    }
}
